//
//  ExamineListViewController.m
//  EMMApp
//
//  Created by Mac on 16/5/5.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "ExamineListViewController.h"
#import "ExamineListCell.h"
#import "GetAuditListApi.h"
#import "KnowDetailsViewController.h"           //认筹
#import "SubscriptionDetailsViewController.h"   //认购
#import "SignDetailsViewController.h"           //签约

@interface ExamineListViewController ()<UITableViewDelegate,UITableViewDataSource>

//定义tableView
@property (nonatomic, strong) UITableView *examineListTableView;

//数据源数组
@property (nonatomic, strong) NSMutableArray *dataArr;

// 当前页码
@property (nonatomic, assign) int currentPageSize;

//当前没有记录
@property (nonatomic, strong) UILabel *jlLabel;

@end

@implementation ExamineListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatNavigationController];
    
    _examineListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, EMM_HEIGHT-SafeAreaTopHeight) style:UITableViewStylePlain];
    _examineListTableView.delegate = self;
    _examineListTableView.dataSource = self;
    _examineListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _examineListTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_examineListTableView];
    
    _jlLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, EMM_WIDTH, 20)];
    _jlLabel.textAlignment = NSTextAlignmentCenter;
    _jlLabel.text = @"当前没有记录";
    _jlLabel.font = [UIFont systemFontOfSize:16];
    _jlLabel.hidden = YES;
    [self.view addSubview:_jlLabel];
    
    self.examineListTableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadNewData];
        [self.examineListTableView.mj_header endRefreshing];
    }];
    self.examineListTableView.mj_header.automaticallyChangeAlpha = YES;
    self.examineListTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
        [self.examineListTableView.mj_footer endRefreshing];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _dataArr = [[NSMutableArray alloc] init];
    [self.examineListTableView.mj_header beginRefreshing];
}


#pragma mark 下拉刷新数据
- (void)loadNewData {
    [_dataArr removeAllObjects];
    _currentPageSize = 1;
    [self postGetAuditListApiPageSize:_currentPageSize];
}

#pragma mark 上拉加载更多数据
- (void)loadMoreData {
    _currentPageSize = _currentPageSize + 1;
    [self postGetAuditListApiPageSize:_currentPageSize];
}

#pragma mark - 结束刷新
- (void)endRefreshing {
    [self.examineListTableView.mj_footer endRefreshing];
    [self.examineListTableView.mj_header endRefreshing];
}

#pragma mark - 审核列表Api
- (void)postGetAuditListApiPageSize:(int)pageSize {
    [self showHud:@"正在加载请稍等..."];
    GetAuditListApi *api = [[GetAuditListApi alloc] initWithPageNumber:_currentPageSize pageSize:10];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        GetAuditListApi *result = (GetAuditListApi *)request;
        if (result.isSuccess) {
            GetAuditList *get = result.resultData;
            for (AuditList *list in get.auditList) {
                [_dataArr addObject:list];
            }

            if (_dataArr.count > 0) {
                _jlLabel.hidden = YES;
            }else {
                _jlLabel.hidden = NO;
            }
            
        } else {
            [self showToast:result.resultData.errorMsg];
            /*
             提示语：账号权限发生变动，请重新登录
             code随机18位唯一码：108E65843mYuwqb01X
             //YUplf0wyU5vPVssLSG 对应的异地登录退出登录
             */
            if ([result.resultData.errorCode isEqualToString:@"YUplf0wyU5vPVssLSG"] || [result.resultData.errorCode isEqualToString:@"108E65843mYuwqb01X"]) {
                NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
                NSDictionary *dictionary = [defs dictionaryRepresentation];
                for(NSString *key in [dictionary allKeys]){
                    if ([key isEqualToString:@"DeviceToken123"] ||
                        [key isEqualToString:@"loginText"] ||
                        [key isEqualToString:@"provinceCityDistrict"]) {
                    }else {
                        [defs removeObjectForKey:key];
                        [defs synchronize];
                    }
                }
                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:result.resultData.errorMsg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                    [appInstance showLogin];
                }];
                [alertController addAction:cancelAction];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
        [self hideHud];
        [self endRefreshing];
        [_examineListTableView reloadData];
    } failure:^(YTKBaseRequest *request) {
        _examineListTableView.hidden = YES;
        _jlLabel.hidden = NO;
        [self showToast:@"连接服务器失败，请检查网络！"];
        [self hideHud];
        [self endRefreshing];
    }];
}


#pragma mark - 导航
- (void)creatNavigationController {
    [self updateNavBar];
    self.navigationItem.title = @"审核列表";
    [self configLeftBtn];
}

#pragma mark - UITableView 代理

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellId = @"ExamineListCellId";
    ExamineListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ExamineListCell" owner:self options:nil] lastObject];
        
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 89, EMM_WIDTH-20, 1)];
    imageView.backgroundColor = [UIColor colorFromHexCode:@"#CCCCCC"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_dataArr.count > 0) {
        [cell addSubview:imageView];
        AuditList *list = [_dataArr objectAtIndex:indexPath.row];
        cell.itemNameLabel.text = list.houses;
        cell.itemPrincipalLabel.text = list.agent;
        cell.itemStatusLabel.backgroundColor = [UIColor orangeColor];
        cell.itemTimerLabel.text = list.createTime;
        cell.itemStatusLabel.layer.masksToBounds = YES;
        cell.itemStatusLabel.layer.cornerRadius = 8;
        cell.itemStatusLabel.text = list.auditType;
        if ([list.auditType isEqualToString:@"认筹"]) {
            cell.itemStatusLabel.backgroundColor = [UIColor colorFromHexCode:@"#F97243"];
        }else if ([list.auditType isEqualToString:@"认购"]) {
            cell.itemStatusLabel.backgroundColor = [UIColor colorFromHexCode:@"#FFA800"];
        }else {
            cell.itemStatusLabel.backgroundColor = [UIColor colorFromHexCode:@"#F82B49"];
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_dataArr.count > 0) {
        AuditList *list = [_dataArr objectAtIndex:indexPath.row];
        if ([list.auditType isEqualToString:@"认筹"]) {
            KnowDetailsViewController *kon = [[KnowDetailsViewController alloc] init];
            kon.businessId = list.businessId;
            kon.taskId = list.taskId;
            kon.isAudit = @"1";
            kon.isNot = YES;
            [self.navigationController pushViewController:kon animated:YES];
        }else if ([list.auditType isEqualToString:@"认购"]) {
            SubscriptionDetailsViewController *kon = [[SubscriptionDetailsViewController alloc] init];
            kon.businessId = list.businessId;
            kon.taskId = list.taskId;
            kon.isAudit = @"1";
            kon.isNot = YES;
            [self.navigationController pushViewController:kon animated:YES];
        }else if ([list.auditType isEqualToString:@"签约"]) {
            SignDetailsViewController *kon = [[SignDetailsViewController alloc] init];
            kon.businessId = list.businessId;
            kon.taskId = list.taskId;
            kon.isAudit = @"1";
            kon.isNot = YES;
            [self.navigationController pushViewController:kon animated:YES];
        }
    }
}

@end
